
#include "includes.h"

int fdFPGA=-1;
int fdTOD=-1;
int fdRbSRO=-1;
int fdEB=-1;
int fdEB2=-1;
int fdDUAL=-1;

int fdtest = -1;
//int fdRLOG=-1;


void set_tty(int fd, int baud, int port)
{
struct termios newtio;
	
	memset(&newtio, 0, sizeof(newtio));

  	cfsetispeed(&newtio, baud);
  	cfsetospeed(&newtio, baud);
	//cfsetospeed(&newtio, baud); 
	
	newtio.c_cflag |= (CLOCAL | CREAD);
    newtio.c_cflag &= ~PARENB;
	newtio.c_cflag &= ~CSTOPB;
	newtio.c_cflag &= ~CSIZE;
	newtio.c_cflag |= CS8;
	newtio.c_cflag &= ~CRTSCTS;
	newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
	newtio.c_iflag &= ~(IXON | IXOFF | IXANY | ICRNL);
	newtio.c_iflag |= IGNPAR;
	newtio.c_oflag &= ~OPOST;
	newtio.c_lflag = 0;

    //newtio.c_cc[VMIN]  = 255;
    //newtio.c_cc[VTIME] = 5;
    newtio.c_cc[VMIN]  = 1;
    newtio.c_cc[VTIME] = 0;

	tcflush(fd, TCIFLUSH);

	//printf("\r\nPort Try...:1");
	//sleep(1);
    if(tcsetattr(fd,TCSANOW,&newtio)<0){
    	sprintf(MESG,"Port setup(%d): %s", port, strerror(errno));
    	LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, MESG);
    	printf("\r\nError:Port setup Fail");
	}
    //else
    	//printf("\r\nPort setup OK");
		
	//sleep(1);

}


void console_recover(void)
{
struct termios newt;

	tcgetattr(STDIN_FILENO, &newt);
	newt.c_lflag |= (ICANON | ECHO);
	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}


int linux_kbhit(void)
{
struct termios newt;
int ch = 0;

	tcgetattr(STDIN_FILENO, &newt);
	newt.c_lflag &= ~(ICANON | ECHO);
	tcsetattr(STDIN_FILENO, TCSANOW, &newt);

	ch = getchar();

	newt.c_lflag |= (ICANON | ECHO);
	tcsetattr(STDIN_FILENO, TCSANOW, &newt);

	return ch;
}

//--------------------------------------------------------------------------------------------------------

int abortboot(int startdelay)
{
int getch1 = 0;
int getch2 = 0;
	
	printf("Hit first key to stop auto start...\n");
	
	alarm(startdelay);
	
	getch1 = linux_kbhit();
//	printf("getch1 = %d\n", getch1);

	if( getch1 == 27 ){ //ESC key
		
		printf("Hit second key to stop auto start...\n");
		
		alarm(startdelay);
		
		getch2 = linux_kbhit();
//		printf("getch2 = %d\n", getch2);
		
		if( getch2 == 10 ){ //Enter key
			return 1;
		}
		else{
			return 0;
		}
	}
	else{
		return 0;
	}

}
